package com.aaa.backsystem.util;

import com.aaa.backsystem.entity.EchartsDataBo;
import com.aaa.backsystem.entity.Sale;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @author ：ckw
 * @date ：Created in 2020/08/21 22:09
 * @description：Echart工具类
 * @modified By：
 * @version: 1.0
 */
public class EchartUtil {

    public static List<EchartsDataBo> findName(List<Sale> sales) {

        List<EchartsDataBo> echartsDataBoList = new ArrayList<>();

        //防止空数据
        if (sales != null && sales.size() > 0) {
            floor:for (int i = 0; i < sales.size(); i++) {

                //已经遍历过得类型不再进行二次遍历
                for (EchartsDataBo echartsDataBo : echartsDataBoList) {
                    if (sales.get(i).getOilType().equals(echartsDataBo.getName())) {
                        continue floor;
                    }
                }

                EchartsDataBo echartsDataBo = new EchartsDataBo();
                //存放类型
                echartsDataBo.setName(sales.get(i).getOilType());
                //存放销量数据
                List<Integer> integers = findData(echartsDataBo.getName(), sales);
                echartsDataBo.setData(integers);

                echartsDataBoList.add(echartsDataBo);
            }
        }
        return echartsDataBoList;
    }

    public static List<Integer> findData(String name, List<Sale> sales) {

        List<Integer> integers = new ArrayList<>(12);

        List<Float> tempData = new ArrayList<>();
        int newMonth = 0;
        int oldMonth = 0;
        boolean flag = true;

        for (int i = 0; i < sales.size(); i++) {
            //判断是否为同一类型
            if (sales.get(i).getOilType().equals(name)) {

                Calendar cal = Calendar.getInstance();
                cal.setTime(sales.get(i).getBuyTime());
                //注意月份是从0开始的,比如当前du7月，获得的month为6
                newMonth = cal.get(Calendar.MONTH) + 1;
                if (flag) {
                    oldMonth = newMonth;
                    flag = false;
                }

                if (newMonth == oldMonth) {
                    tempData.add(sales.get(i).getBuyNum());

                } else {
                    Float sum = 0F;

                    for (Float temp : tempData) {
                        //将同一月份的销售量相加
                        sum += temp;
                    }

                    addList(integers, oldMonth-1);

                    tempData.removeAll(tempData);
                    tempData.add(sales.get(i).getBuyNum());
                    oldMonth = newMonth;

                    //将同一类型销量数据存起来
                    integers.add(sum.intValue());
                }

            }

            if (i == sales.size() - 1) {
                if (tempData != null && tempData.size() > 0) {
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(sales.get(i).getBuyTime());
                    //注意月份是从0开始的,比如当前du7月，获得的month为6
                    int Month = cal.get(Calendar.MONTH);

                    Float sum = 0F;

                    for (Float temp : tempData) {
                        //将同一月份的销售量相加
                        sum += temp;
                    }
                    addList(integers, Month);
                    //将同一类型销量数据存起来
                    integers.add(sum.intValue());
                }
            }
        }
        return integers;
    }


    public static   List<Integer> addList(List<Integer> integers,Integer num){
       int count = num - integers.size();
       for(int i=0;i<count;i++){
           integers.add(0);
       }
        return integers;
    }
}
